from Products.KnowledgeEngine.KERL.Engine import Engine
from Products.KnowledgeEngine.Assessment import Assessment

model = context.getComponentByIdentity( "model01167883734167037280186" )
ass   = context.knowledgeengine_assessmentstorageservice

assessmentData = dict(
    mch01167884253996246199746  = 'A',
    opt01167887774682365486135  = True,
    opt011678877910618701860094 = False,
    opt01168314443285759289496  = True,
    opt0116831444327419078891   = False,
    opt01168316098187235292417  = False,
    opt011683150180988517702903 = True,
    opt01168315018148034323487  = False,
    opt01168317126103066644271  = True,
    opt01168317126174917269269  = False,
    chk01168319204865487035067  = True,
    text0116849541117151998027  = 'golf',
    num01168496336889013618371  = 7.8,
    num0117082392432244483805   = 5.0,
    num0117082750171339253654   = 6.6,
)

testIdentity = 'kerlTestAssessment'

ass.deleteAssessmentWith( testIdentity, model.getIdentity() )

assessment = Assessment( testIdentity, model )

for componentIdentity, value in assessmentData.items():
    
    state = assessment.getComponentState( componentIdentity )
    state.setValue( value )
    

current_user_id = context.REQUEST.AUTHENTICATED_USER.getId()

# [ (Kerl, Expectation) ]
tests = [
    ( ['mch01167884253996246199746','selected','A'],   True),
    ( ['opt01167887774682365486135','unselected'],     False),
    ( ['opt01167887774682365486135','selected'],       True),
    ( ['text0116849541117151998027','equals','golf'],   True),
    ( ['text0116849541117151998027','equals','hockey'], False),
    ( ['text0116849541117151998027','contains','olf'], True),
    ( ['all',['opt01167887774682365486135','selected'],
             ['opt011678877910618701860094','unselected']], True)
             
    ( ['currentuser', 'is', current_user_id ], True ), 

]             

engine = Engine()

print "<table>"

for kerl, expected in tests:
    
    compiled  = engine.compile( kerl, model=model, component=None )
    optimized = compiled.optimize( assessment )
    
    result = optimized.execute( assessment )
    
    if result is expected:
        success = "success"
    else:
        success = "failure"
        
    print "<tr><td>%s</td><td>%s</td></tr>" % ( kerl, success )
    
print "</table>"
    
    
        
        
return printed        
        
    
